package com.yazi.mxz.common.db.dao.impl;


import java.util.HashMap;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.yazi.mxz.common.db.dao.ArticleDao;
import com.yazi.mxz.common.entity.beans.Article;
import com.yazi.mxz.common.entity.beans.Category;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.dao.BasicDao;

@Repository("articleDao")
public class ArticleDaoImpl extends BasicDao<Article> implements ArticleDao {

	public ArticleDaoImpl() {
		super(Article.class);
		
	}

	@Override
	public Criteria getExample(Article article, String searchWord) {
		Criteria c = super.getExample(article, searchWord);
		
		// 关联对象条件
		if (null != article) {
			// 分类
			Category category = article.getCategory();
			if (null != category) {
				// 分类ID
				if (null != category.getCatId()) {
					c.add(Restrictions.eq("category.catId", category.getCatId()));
				} else {
					if (null != category.getType()) {
						c.add(Restrictions.eq("category.type", category.getType()));
					}
					// ...分类其它条件
				}
			}
			// ...其它关联对象条件
		}
		
		// 关键字条件
		if (null != searchWord && !searchWord.isEmpty()) {
			// 关键字可查标题,摘要,正文
			String likeWord = "%" + searchWord + "%";
			c.add(Restrictions.or(
					Restrictions.like("title", likeWord),
					Restrictions.or(Restrictions.like("summary", likeWord),
							Restrictions.like("body", likeWord))));
		}
		
		return c;
	}

	@Override
	public Page<Article> listByRootCatId(Long catId, Searching si) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("cat_id", catId);
		return list("listByRootCatIdSql", map, si);
	}

	@Override
	public void deleteByStoreId(long storeId, long[] articleIds) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("storeId", storeId);
		map.put("articleIds", articleIds);
		executeUpdate("delByStoreIdHql", map);
	}

}
